<template>
    <el-dialog v-model="companyDataStore.joinDialogVisible" width="580" :show-close="false" :z-index="9">
        <template #header>
            <div class="dialog-header">
                <h4>加入企业</h4>
                <div class="tip">
                    <span>加入企业后，将成为企业的员工，可与企业进行内部线上签署</span>
                </div>
            </div>
        </template>
        <div class="dialog-content">
            <div class="label">请输入您的企业名称，并确保与营业执照上的企业名称一致</div>
            <el-select v-model="companyName" filterable remote reserve-keyword placeholder="请输入企业名称"
                :remote-method="remoteMethod" :loading="loading">
                <el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.name" />
            </el-select>
            <!-- <el-input v-model="companyName" placeholder="请输入企业名称" /> -->
        </div>
        <template #footer>
            <div class="dialog-foot">
                <el-button @click="companyDataStore.joinDialogVisible = false">取消</el-button>
                <el-button type="primary" :loading="btnLoading" @click="handleJoin">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { JoinCompany, SearchCompany } from '@/api/enterprise'
import { ElMessage } from 'element-plus'
import { useCompanyDataStore } from '@/store/companys'

const companyDataStore = useCompanyDataStore()

const btnLoading = ref(false)
const companyName = ref('')

interface ListItem {
    id: number
    name: string
}

const companyList = ref<ListItem[]>([])
const loading = ref(false)

const handleJoin = () => {
    if (!companyName.value.trim()) {
        return ElMessage.error('企业名称不能为空')
    }
    btnLoading.value = true
    JoinCompany({
        name: companyName.value.trim()
    }).then(res => {
        btnLoading.value = false
        ElMessage.success('申请成功,等待审核')
        companyName.value = ''
        companyDataStore.joinDialogVisible = false
    }).catch(() => {
        btnLoading.value = false
    })
}

const remoteMethod = (query: string) => {
    if (query) {
        loading.value = true
        SearchCompany({
            name: query
        }).then(res => {
            loading.value = false
            companyList.value = res.data
        })
    } else {
        companyList.value = []
    }
}

</script>
<style scoped lang="scss">
.dialog-header {
    display: flex;
    flex-direction: column;
    padding: 10px;

    h4 {
        font-size: 18px;
        color: #333;
        font-weight: normal;
    }

    .tip {
        font-size: 16px;
        color: #999;
        background-color: #f3f8fb;
        display: flex;
        align-items: center;
        padding: 3px 5px;
        border-radius: 5px;
        margin-top: 10px;

        span {
            margin-left: 5px;
        }
    }
}

.dialog-content {
    padding: 0 10px;

    .label {
        font-size: 16px;
        color: #666;
        margin-bottom: 20px;
    }
}

.dialog-foot {
    padding: 10px;
}
</style>